<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 机器人下单脚本
 */
class Cron_Robot extends CI_Controller {
	private $redis;
	private $cfdClass;
	private $logFile;
	public function __construct(){  
        parent::__construct();
        if(!is_cli()){
        	header("HTTP/1.0 404 Not Found");
        	show_error('Not Found',404);
        	die;
        }

        //加载操作类
		$this->load->config('cfd');
		$this->load->library('cfdClass');
		$this->cfdClass = $this->cfdclass;
    }

    /**
     * 机器人注册
     * @return [type] [description]
     */
	public function reg(){
		$phone		= isset($post['phone'])?strtolower(trim($post['phone'])):'';
        $password	= 'robot123456';
        $password2	= 'robot123456';
        $vaildCode	= 'robot123456';
		$platform	= $this->config->item('platform');//平台
        $cooper		= $this->config->item('cooper');
		$register	= 1;
		$area		= 86;
		$lang		= 'zh-cn';
		$comefrom	= 1;//1pc  2手机
		$reg_flag	= 1;//注册
		$datetime	= date('Y-m-d H:i:s');

		$this->load->model('user_model');
		for($phone='10000000001';$phone<='10000000101';$phone++){
			$openid	=	md5($phone);
			$whereArr = array(
				'platform' => $platform,//平台
				'phone' => $phone,
			);
			$isExist = $this->user_model->getUserInfoByMulti($whereArr);
			if($isExist){
				continue;
			}

			$whereArr = array(
				'platform' => $platform,//平台
				'openid' => $openid,
			);
			$isExistWX = $this->user_model->getUserInfoByMulti($whereArr);
			if(!empty($isExistWX)){
				continue;
			}
			
			$ip		= '127.0.0.1';
			$time	= time();
			$token	= $phone;
			$signmsg= strtoupper(md5($platform . $cooper . $this->config->item('cooper_key') . $phone));

			$otpArr = array(
				'register' => $register,//注册操作
				'area' => $area,
				'phone' => $phone,
				'token' => $token,
				'password' => md5(md5($password.$ip.$time)),
				'signmsg' => $signmsg,
				'time' => $time,
				'user_ip' => $ip,
				'lang' => $lang,
				'comefrom' => $comefrom,
				'reg_flag' => $reg_flag,
				'openid' => $openid,
			);

			$this->load->library('passPort');
			$this->passport->setPlatform($platform);
			$this->passport->setCooper($cooper);
			$rs = $this->passport->getOtp($otpArr);
			
			//解析获取的结果
			$result = array();
			parse_str($rs, $result);
			if ($result['result'] == '1001') {
				//验证是否正确
				$verify_msg = strtoupper(md5($platform . $cooper . $token . '1001' . $result['otp'] . $this->config->item('cooper_key')));
				if($result['signmsg'] != $verify_msg) {
					continue;
				}
				$uid = $result['uid'];
				//登录验证
				$signmsg    =   strtoupper(md5($platform . $cooper . $this->config->item('cooper_key') . $token . $result['otp']));
				/***********注册记录LOG Start************/
				$regLogArr  =   array(
					'uid'           =>  $uid,
					'platform'      =>  $platform,
					'cooper'        =>  $cooper,
					'comefrom'      =>  $comefrom,//网站
					'system'        =>  getSystem(),
					'browser'       =>  getBrowser(),
					'ip'            =>  $ip,
					'create_time'   =>  $datetime,
				);
				$this->user_model->addRegLog($regLogArr);
			}
		}
	}

			
	//机器人下单
	public function order(){
		$this->load->model('user_model');
		$user_arr	=	$this->user_model->getUserPhone("10000000001","10000000030");

		$rand_arr	=	array_rand($user_arr, 5);
		$user_arr	=	$this->open($rand_arr,$user_arr,4);

		$rand_arr	=	array_rand($user_arr, 5);
		$user_arr	=	$this->open($rand_arr,$user_arr,3);

		$rand_arr	=	array_rand($user_arr, 10);
		$user_arr	=	$this->open($rand_arr,$user_arr,2);

		$rand_arr	=	array_rand($user_arr, 10);
		$user_arr	=	$this->open($rand_arr,$user_arr,1);
	}

	private function open($rand_arr,$user_arr,$num) {
		$dtype	=	'ticket';
		$com	=	5;
		$type	=	0;//0市价 1限价
		$times	=	10;//每手交易保证金
		$price	=	0;//0市价，其他限价
		$datetime	= date('Y-m-d H:i:s');

		$this->load->model('product_model');
		if(!empty($rand_arr)) foreach($rand_arr as $keys){
			$user	=	$user_arr[$keys];
			unset($user_arr[$keys]);
			
			if(!empty($user)){
				$action_arr	= array("ask", "bid");
				$action_keys= array_rand($action_arr, 1);
				$action		= $action_arr[$action_keys];

				$pid_arr	= array("12", "13","14","15");
				$pid_keys	= array_rand($pid_arr, 1);
				$pid		= $pid_arr[$pid_keys];
				
				//获取产品详细信息

				$productData = $this->product_model->getProduct(array('pid'=>$pid, 'status'=>1),1);
				if(empty($productData)){
					writeDebug('['.$datetime.'] 获取产品详细信息失败！', 'Cron_Robot');
					exit;
				}

				if(!$this->cfdClass->setProduct($productData['name'],'IB')){//设置产品
					writeDebug('['.$datetime.'] 设置产品失败！', 'Cron_Robot');
					exit;
				}

				if(!$this->cfdClass->setDenom($dtype)){//设置计价方式
					writeDebug('['.$datetime.'] 设置计价方式失败！', 'Cron_Robot');
					exit;
				}

				if(!$this->checkTime() || !$this->checkFlag()) {
					writeDebug('['.$datetime.'] 不在交易时间内！', 'Cron_Robot');
					die("不在交易时间内！");
				}
				
				$data = array(
					'uid' => $user['uid'],
					'pid' => $pid,
					'dtype' => $dtype,
					'type' => $type,
					'times' => $times,
					'action' => $action,
					'price' => $price,
					'num' => $num,
					'com' => $com,
				);

				$rs = $this->cfdclass->open($data);
				$rs = json_decode($rs,true);
				if($rs['errno']==0){
					writeDebug('['.$datetime.'] UID:'.$user['uid'].'机器人下单成功！', 'Cron_Robot');
				}else{
					writeDebug('['.$datetime.'] UID:'.$user['uid'].'机器人下单失败！:'.$rs['msg'], 'Cron_Robot');
				}
			}
		}
		return $user_arr;
	}

	/**
	 * 检查是否在交易时间内
	 * @return boolean
	 */
	private function checkTime() {
		$rs = $this->cfdclass->checkTime();
		return $rs;
	}

    public function checkFlag(){
        $tradeData = $this->cfdclass->redisAllData();
        if(!isset($tradeData['trade']['FLAG']) || $tradeData['trade']['FLAG']==0){//不能交易
        	return false;
        }
        return true;
	}
}
